package com.zcsy.manage.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.system.Button;
import com.zcsy.manage.model.system.RoleButton;
import com.zcsy.manage.persistence.system.ButtonDao;
import com.zcsy.manage.service.system.IButtonService;

/**  
 * @Project: service
 * @Title: ButtonServiceImpl.java
 * @Package com.zcsy.shop.service.system.impl
 * @Description: TODO
 *
 * @author xiangjw 303310059@qq.com
 * @date 2015年8月12日 下午5:03:04
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @version V1.0  
 */
@Service
public class ButtonServiceImpl extends SystemComService implements IButtonService {
	
	@Autowired
	private ButtonDao buttonDao;

	@Override
	public List<Button> queryRoleButton(String roleId) {
		List<Button> allBtns = buttonDao.queryAllButton();
		List<Button> roleBtns = buttonDao.queryRoleButton(roleId);
		
		//是否选中，赋值
		for(Button item : allBtns){
			boolean isCheck = false;
			for(Button roleItem : roleBtns){
				if(roleItem.getId() == item.getId()){
					isCheck = true;
					break;
				}
			}
			
			item.setCheck(isCheck);
		}
		
		return allBtns;
	}
	
	@Override
	public List<Button> getRoleButtonsFromCache(String roleId) {
		List<Button> roleBtns = new ArrayList<Button>();
		roleBtns = buttonDao.queryRoleButton(roleId);
		
		return roleBtns;
	}

	@Override
	public void updateRoleButton(Button[] buttons, String roleId) {
		List<Button> oldBtns = buttonDao.queryRoleButton(roleId);
		List<String> delList = new ArrayList<String>();
		List<RoleButton> addList = new ArrayList<RoleButton>();
		//新的有老的没有，即为添加
		for(Button item : buttons){
			boolean isEqual = false;
			for(Button oldItem : oldBtns){
				if(oldItem.getId() == item.getId()){
					isEqual = true;
					break;
				}
			}
			
			if(!isEqual){
				RoleButton btn = new RoleButton();
				btn.setButtonId(item.getId());
				btn.setRoleId(roleId);
				btn.setId(UniqueNumberUtils.get18UniqueNumber());
				addList.add(btn);
			}
		}
		//新的没有老的有，即为删除
		for(Button oldItem : oldBtns){
			boolean isEqual = false;
			for(Button item : buttons){
				if(oldItem.getId() == item.getId()){
					isEqual = true;
					break;
				}
			}
			
			if(!isEqual){
				delList.add(oldItem.getRoleBtnId());
			}
		}
		
		if(addList.size() > 0){
			buttonDao.addRoleButton(addList);
		}
		
		if(delList.size() > 0){
			buttonDao.delRoleButton(delList);
		}
	}

	@Override
	public List<Button> queryButton(Button btn) {
		return buttonDao.queryButton(btn);
	}

	@Override
	public int insert(Button record) {
		return buttonDao.insert(record);
	}

	@Override
	public int updateByPrimaryKey(Button record) {
		return buttonDao.updateByPrimaryKey(record);
	}

	@Override
	public int deleteByPrimaryKeys(String[] ids) {
		return buttonDao.deleteByPrimaryKeys(ids);
	}

	@Override
	public int checkIfButtonUsed(String id) {
		return buttonDao.checkIfButtonUsed(id);
	}

    @Override
    public Button selectByPrimaryKey(String id) {
        return buttonDao.selectByPrimaryKey(id);
    }
}
